export default {
  state: {
    infors: {},
  },
  mutations: {
    // 添加发送信息
    SENDADDINFOR(state, value) {
      // 检测该接收者的电话是否存在
      if (value.sendee in state.infors) {
        // 存在，向该数组push发出的信息
        // state.infors[value.sendee].push(value);
        state.infors[value.sendee].text.push(value);
      } else {
        // 不存在，以该电话号码新建数值，储存消息
        // state.infors[value.sendee] = [];
        state.infors[value.sendee] = { tiem: 0, text: [] };
        state.infors[value.sendee].text.push(value);
      }
      state.infors[value.sendee].text.sort((a, b) => a.sendtime - b.sendtime);
    },

    // 添加接收信息
    RECEIVEADDINFOR(state, value) {
      if (value.sender in state.infors) {
        state.infors[value.sender].text.push(value);
      } else {
        state.infors[value.sender] = { tiem: 0, text: [] };
        state.infors[value.sender].text.push(value);
      }
      state.infors[value.sender].text.sort((a, b) => a.sendtime - b.sendtime);
    },
    // 改变时间
    CHOOSETIME(state, value) {
      const lasttime = Date.now();
      if (value in state.infors) {
        state.infors[value].time = lasttime;
      } else {
        state.infors[value] = { tiem: 0, text: [] };
        state.infors[value].time = lasttime;
      }
    },
  },
  actions: {},
  getters: {
    lastinfor: (state) => (callcode) => {
      if (state.infors[callcode] && state.infors[callcode].text.length > 0) {
        return state.infors[callcode].text[state.infors[callcode].text.length - 1];
      }
      return { infortext: '  ', sendtime: '  ' };
    },
    // nowatchinfor: (state) => (callcode) => {
    //   // 最后一次看消息的时间戳
    //   if (state.infors[callcode]) {
    //     const lasttime = state.infors[callcode].time;
    //     const ary = state.infors[callcode].text.filter((item) => item.sendtime > lasttime);
    //     return ary.length;
    //   }
    //   return 0;
    // },
  },
  modules: {},
};
